//201902104011王雪梦
package bysj.service;
import bysj.dao.DepartmentDao;
import bysj.domain.Department;
import bysj.domain.School;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class DepartmentService {
    private static DepartmentDao departmentDao= DepartmentDao.getInstance();
    private static DepartmentService departmentService= null;
    private DepartmentService(){}

    public static DepartmentService getInstance(){
        if(departmentService == null){
            departmentService = new DepartmentService();
        }
        return departmentService;
    }
    public int countAll(School school) throws SQLException {
        return departmentDao.countAll(school);
    }
    public Collection<Department> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Department> departments = departmentDao.findAll(connection,conditionList,pagination);
        return departments;
    }

    public Department find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Department department = departmentDao.find(id,connection);
        JdbcHelper.close(connection);
        return department;
    }

    public boolean update(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = departmentDao.update(department,connection);
        JdbcHelper.close(connection);
        return updated;
    }

    public boolean add(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = departmentDao.add(department,connection);
        JdbcHelper.close(connection);
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = departmentDao.delete(id,connection);
        JdbcHelper.close(connection);
        return deleted;
    }
}

